home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 3_0 / OIC_1 / COORD.C < prev    next >
Text File  |  1989-03-01  |  1KB  |  95 lines

  1. /*
  2.  *            quick-n-dirty class for the Objects-in-C tester
  3.  *
  4.  *            Copyright ⌐ John Wainwright 1988
  5.  *
  6.  */
  7.  
  8. #include "oic.h"
  9. #include "generics.h"
  10.  
  11. typedef struct                    /* coord IVs                            */
  12. {
  13.     float    x;
  14.     float    y;
  15. } coord_i;
  16. typedef object coord;
  17.  
  18. class    Coord;
  19.  
  20. /* ------------------------------ Coord Ops -------------------------- */
  21.  
  22. static coord
  23. _new(self, c, args)
  24.     coord    self;
  25.     coord_i    *c;
  26.     struct
  27.     {
  28.         double    x;
  29.         double  y;
  30.     } *args;
  31. {
  32.     c->x = args->x;
  33.     c->y = args->y;
  34.     return Super(self);
  35. }
  36.  
  37. static coord
  38. _offset(self, c, args)
  39.     coord    self;
  40.     coord_i    *c;
  41.     struct { double dx, dy; } *args;
  42. {
  43.     c->x += args->dx;
  44.     c->y += args->dy;
  45.     return self;
  46. }
  47.  
  48. static int
  49. _xCoord(self, c)
  50.     coord    self;
  51.     coord_i    *c;
  52. {
  53.     return c->x;
  54. }
  55.  
  56. static int
  57. _yCoord(self, c)
  58.     coord    self;
  59.     coord_i    *c;
  60. {
  61.     return c->y;
  62. }
  63.  
  64. static string
  65. _replist(self, c)
  66.     coord    self;
  67.     coord_i *c;
  68. {
  69.     char s[100];
  70.  
  71.     sprintf(s, "[%.2f, %.2f]", c->x, c->y);
  72.     return New(String, s);
  73. }
  74.  
  75. /* ------------------------------ Init Class ----------------------- */
  76.  
  77. externGeneric(xCoord,    xCoordGeneric)
  78. externGeneric(yCoord,    yCoordGeneric)
  79. externGeneric(offset,    offsetGeneric)
  80.  
  81. defGeneric(xCoord,    xCoordGeneric,    "xCoord")
  82. defGeneric(yCoord,    yCoordGeneric,    "yCoord")
  83.  
  84. InitCoordClass()
  85. {
  86.     Coord = NewClass(sizeof(coord_i), 0, "Coord", IndexMixin, END);
  87.     AddMethods(Coord,
  88.         newGeneric,            _new,
  89.         offsetGeneric,        _offset,
  90.         repListGeneric,        _replist,
  91.         xCoordGeneric,        _xCoord,
  92.         yCoordGeneric,        _yCoord,
  93.         END);
  94. }
  95.